package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "购物车相关接口")
@RestController("UserShoppingCarController")
@RequestMapping("/user/shoppingCart")
@Slf4j
public class ShoppingCarController {
    @Autowired
    private ShoppingCarService shoppingCarService;

    /**
     *添加购物车
     *
     * @param shoppingCartDTO
     * @return
     */
    @ApiOperation("添加购物车")
    @PostMapping("/add")
public Result save(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加购物车{}",shoppingCartDTO);
 shoppingCarService.add(shoppingCartDTO);
                    return Result.success();
    }

    @ApiOperation("查看购物车")
    @GetMapping("/list")
    public Result list(){
        log.info("查看购物车");
 List<ShoppingCart> shoppingCartList= shoppingCarService.select();
 return Result.success(shoppingCartList);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @ApiOperation("清空购物车")
    @DeleteMapping("clean")
    public Result clean(){
        log.info("清空购物车");
        shoppingCarService.delete();
        return Result.success();

    }
   @ApiOperation("删除购物车的一个商品")
    @PostMapping("/sub")
    public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("删除购物车的一个商品{}",shoppingCartDTO);
        shoppingCarService.deleteBy(shoppingCartDTO);
        return Result.success();
   }
}
